
$(function() {	
	$("input[id='emp']:visible").autocomplete(Test.emp);
	
	_ck_default("input[id='emp']:visible", "input[id^='emp_t']:visible");
	
	$('#date').datepicker(_now_option);
	
	$('#customer').change(function() {
		var a = $(this).val();
		// Pay.supplier(a);
		
		var body = { };
		body.id = a;
		$.getJSON('contact.php?do=Customer.ajax_id', body, function(json) {
			$("#a_name").val(json.name);
			$("#a_fullname").val(json.fullname);
			$("#a_address").val(json.address);
			$("#a_web").val(json.url);
			
			/*
			var s = $("#name").val();
			if (s == null || s == '') {
				$("#name").val(json.code);
			}*/
			
			// $("#emp").val(json.emp);
		});
		
		var body = {};
		body.q = a;
		
		$.getJSON('contact.php?do=Customer.ajax_linkman', body, function(json) {	
			// alert($.toJSON(json));
			Common.linkman(json);
		});
	});
	
	$("#label_linkman_a").hide();
	
	Tip.supplier();
	
	$('#supplier').blur(function() {		
		var a = $(this).val();
		
		var body = { };
		body.id = a;
		$.getJSON('contact.php?do=Supplier.ajax_id', body, function(json) {
			$("#c_name").val(json.name);
			$("#c_fullname").val(json.fullname);
			$("#c_address").val(json.address);
			$("#c_web").val(json.url);
			
			$("#currency").val(json.currency);
			$("#type").val(json.pay);
			$("#tax").val(json.tax);
		});
		
		var body = {};
		body.q = a;
		
		$.getJSON('contact.php?do=Supplier.ajax_linkman', body, function(json) {			
			Supplier.linkman(json);
		});
	});
		
	$("#label_linkman").hide();
	
	_change("input[id='type']:visible", "select[id^='type_t']:visible");
	
	// _change("input[id='address']:visible", "select[id^='address_t']:visible");
	_change("#address", "select[id^='address_t']:visible");
	
	$('textarea').css("width", 400);
	
	total();
	
	var options = {
			editable: true,
		    enableAddRow: true,
		    enableCellNavigation: true,
		    asyncEditorLoading: false,
		    rowHeight: 30
	};	
	
	var grid = new Slick.Grid($("#myGrid"), data, columns, options);
	
	grid.onAddNewRow.subscribe(function (e, args) {
        var item = {unit2:"PCS", amount2:"1.00", price2:"1.00", unit:"PCS", amount:"1.00", price:"1.00", tax: "0", ok: true};
        
        item.total = fxmul(item.amount2, item.price2);
        
        $.extend(item, args.item);
        data.push(item);
        grid.invalidateRows([data.length - 1]);
        grid.updateRowCount();
        grid.render();
        
    	total();
    });
	   
	grid.onCellChange.subscribe(function(e, args) {
		item = args.item;
		currentCell = args.cell;
		currentRow = args.row;
		
		item.total = fxmul(item.amount2, item.price2);
    	
    	if (item.unit == item.unit2) {
    		item.amount = item.amount2;
    	}
    	
    	grid.updateRow(currentRow);
    	grid.updateRowCount();
    	grid.render();
    	
    	total();
    });
});

function total() {
	var total = 0;
	$.each(data, function(k, v) {
		if (v.ok) {
			v.amount2 = parseFloat(v.amount2);
			v.price2 = parseFloat(v.price2);
			v.tax = parseFloat(v.tax);
			v.amount = parseFloat(v.amount);
			
			v.total = fxmul(v.amount2, v.price2);
			// pay += parseFloat(v.pay);
			total = fxadd(total, v.total);			
		}		
	});
	
	$("#total_t").html(total);
	
	$("#total").val(total);
}
